package com.ymatou.auth.testcase;

import java.io.IOException;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.auth.parameter.LockIpRequestDtoBean;
import com.ymatou.auth.parameter.LoginAuthBean;
import com.ymatou.auth.parameter.UnLockIpRequestDtoBean;
import com.ymatou.auth.service.LockIpRequestDtoCall;
import com.ymatou.auth.service.LoginAuthCall;
import com.ymatou.auth.service.UnLockIpRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.data.LoginSafeConfiguration;
import com.ymttest.business.service.AuthCallService;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.mongo.YmatouBlacklistUserWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;
/**
*解锁IP接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UnLockIpRequestDto{
	private static UnLockIpRequestDtoBean unlockiprequestdtoBean;
	private static UnLockIpRequestDtoCall unlockiprequestdtoCall;
	private static YmatouBlacklistUserWapper blackLisWapper;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("解锁IP接口");
	}

	@Before
	public void caseUp() throws Exception {
		unlockiprequestdtoBean=new UnLockIpRequestDtoBean();
		unlockiprequestdtoCall=new UnLockIpRequestDtoCall();
		blackLisWapper = new YmatouBlacklistUserWapper();
	}

	@AfterClass
	public static void tearDown() throws Throwable {
		Logger.generateResult(unlockiprequestdtoCall.getOpurl());
		blackLisWapper.close();
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UnLockIpRequestDto_001() {
		Logger.start(true,"解锁买家单个IP，验证IP已解锁");
		try {
			String[] ips = new String[]{"172.12.101.68"};
			lockIPs(ips);
			unlockiprequestdtoBean.setIp(ips);
			unlockiprequestdtoBean.setOperating("解锁 IP");
			unlockiprequestdtoBean.setDescription("解锁 IP");
			unlockiprequestdtoBean.setIpType(1);
			unlockiprequestdtoCall.setData(unlockiprequestdtoBean);
			unlockiprequestdtoCall.callService();			
			verifyIpsStatus(ips);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	//TODO 解锁多个ip传数组是有时会失效,前面需要控制不传多个
	public void Tc_UnLockIpRequestDto_002() {
		Logger.start(true,"解锁多个IP，验证IP已解锁");
		try {
			Logger.comment("注册一个新用户");
			Map UserMap =UserCallService.registerByMobile();
			String AccessToken = (String) UserMap.get("accesstoken");
			int UserId = (Integer) UserMap.get("userid");
			String UserName = (String) UserMap.get("username");
			
			String[] ips = new String[2];
			for(int i=0; i<2; i++){
				ips[i] = TestDataManager.getRandomIp();
			}
			lockIPs(ips);
			//登录
			Thread.sleep(1000);
			Logger.comment("开始登陆，验证IP被锁定");
			for (String ip:ips){
				LoginAuthBean lab = new LoginAuthBean();
				LoginAuthCall lac = new LoginAuthCall();
				lab.setLoginId(UserName);
				lab.setPas("123456a");
				lab.setClientIp(ip);
				lab.setSourceType("mainsite");
				lac.setData(false,lab);
				AuthCallService.authIPArea(ip, UserName, "123456a", "mainsite");
				lac.callService();
				LoginSafeConfiguration.checkLoginStatusWithBlackIp(lac, false, null);
			}
			
			Logger.comment("解锁IP");
			unlockiprequestdtoBean.setIp(ips);
			unlockiprequestdtoBean.setOperating("解锁 IP");
			unlockiprequestdtoBean.setDescription("解锁 IP");
			unlockiprequestdtoCall.setData(unlockiprequestdtoBean);
			unlockiprequestdtoCall.callService();			
			verifyIpsStatus(ips);
			
			//登录
			Logger.comment("开始登陆，验证用户被锁定");
			for (String ip:ips){
				LoginAuthBean lab = new LoginAuthBean();
				LoginAuthCall lac = new LoginAuthCall();
				lab.setLoginId(UserName);
				lab.setPas("123456a");
				lab.setClientIp(ip);
				lab.setSourceType("mainsite");
				lac.setData(false,lab);
				AuthCallService.authIPArea(ip, UserName, "123456a", "mainsite");
				lac.callService();
				Logger.verifyEquals(true, lac.getSuccess(), "验证登陆成功");		
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UnLockIpRequestDto_003() {
		Logger.start(false,"不输入IP，解锁失败");
		try {
			unlockiprequestdtoBean.setOperating("解锁 IP");
			unlockiprequestdtoBean.setDescription("解锁 IP");
			unlockiprequestdtoCall.setData(unlockiprequestdtoBean);
			unlockiprequestdtoCall.callService();
			Logger.verifyEquals(false, unlockiprequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals("fail", unlockiprequestdtoCall.getResult(), "验证结果信息");
			Logger.verifyEquals(ECode.AUTH_NOIP2, unlockiprequestdtoCall.getMsg(), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UnLockIpRequestDto_004() {
		Logger.start(false,"不输入操作，解锁失败");
		try {
			String[] ips = new String[]{"172.12.101.68"};
			unlockiprequestdtoBean.setIp(ips);
			unlockiprequestdtoBean.setDescription("解锁 IP");
			unlockiprequestdtoCall.setData(unlockiprequestdtoBean);
			unlockiprequestdtoCall.callService();
			Logger.verifyEquals(false, unlockiprequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals("fail", unlockiprequestdtoCall.getResult(), "验证结果信息");
			Logger.verifyEquals(ECode.AUTH_NOOPERATION_DESCRIPTION, unlockiprequestdtoCall.getMsg(), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UnLockIpRequestDto_005() {
		Logger.start(false,"不输入操作描述，解锁失败");
		try {
			String[] ips = new String[]{"172.12.101.68"};
			unlockiprequestdtoBean.setIp(ips);
			unlockiprequestdtoBean.setOperating("解锁 IP");
			unlockiprequestdtoCall.setData(unlockiprequestdtoBean);
			unlockiprequestdtoCall.callService();
			Logger.verifyEquals(false, unlockiprequestdtoCall.getSuccess(), "验证是否成功");
			Logger.verifyEquals("fail", unlockiprequestdtoCall.getResult(), "验证结果信息");
			Logger.verifyEquals(ECode.AUTH_NOOPERATION_DESCRIPTION, unlockiprequestdtoCall.getMsg(), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UnLockIpRequestDto_006() {
		Logger.start(true,"解锁买手单个IP，验证IP已解锁");
		try {
			String ip="001.004.000.255";
			String[] ips = new String[]{ip};
			AuthCallService.authLogin("洋码头", "1234561", "seller.app.ymatou.com", ip);
			AuthCallService.authLogin("洋码头", "123456a", "seller.app.ymatou.com", ip);
			unlockiprequestdtoBean.setIp(ips);
			unlockiprequestdtoBean.setOperating("解锁 IP");
			unlockiprequestdtoBean.setDescription("解锁 IP");
			unlockiprequestdtoBean.setIpType(2);
			unlockiprequestdtoCall.setData(unlockiprequestdtoBean);
			unlockiprequestdtoCall.callService();
			Logger.verifyEquals("ok", unlockiprequestdtoCall.getResult(), "验证结果信息");
			Logger.verifyEquals("true", unlockiprequestdtoCall.getString("Success"), "验证Msg信息");
			//手工验证
			// a. 根据用户IP来源（买家IP、买手IP），删除mongo中的黑名单IP（YmatouBlacklist_Ip_Seller、YmatouBlacklist_Ip）。--保证登录不会因为IP黑名单被拦截
			boolean haslock=new YmatouBlacklistUserWapper().hasSellerIPLocked(ips[0]);
			Logger.verifyEquals(false, haslock, "验证YmatouBlacklist_Ip_Seller清除数据");
			// b. 将用户登录Ip计数表（loginipcountinfo、loginipcountinfo_seller）中此IP的最近一次“成功”和“失败”计数（对应字段：LoginCount）清零。--保证不会因为之前的登录Ip计数而再次将IP加入黑名单
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UnLockIpRequestDto_007() {
		Logger.start(false,"不输入IpType,兼容旧接口");
		try {
			String[] ips = new String[]{"172.12.101.68"};
			lockIPs(ips);
			unlockiprequestdtoBean.setIp(ips);
			unlockiprequestdtoBean.setOperating("解锁 IP");
			unlockiprequestdtoBean.setDescription("解锁 IP");
//			unlockiprequestdtoBean.setIpType(1);
			unlockiprequestdtoCall.setData(unlockiprequestdtoBean);
			unlockiprequestdtoCall.callService();
			Logger.verifyEquals("ok", unlockiprequestdtoCall.getResult(), "验证结果信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	/**
	 * 锁住指定IPs
	 * @param ips
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	private void lockIPs(String[] ips) throws ClientProtocolException, IOException{
		Logger.comment("锁住指定的IP地址:"+ips.toString());
		LockIpRequestDtoBean lockiprequestdtoBean = new LockIpRequestDtoBean();
		LockIpRequestDtoCall lockiprequestdtoCall = new LockIpRequestDtoCall();
		
		lockiprequestdtoBean.setIp(ips);
		lockiprequestdtoBean.setOperation("lock IP");
		lockiprequestdtoBean.setDescription("锁住Ip");
		lockiprequestdtoBean.setLockdTimeOut("PT12H");
		lockiprequestdtoCall.setData(false,lockiprequestdtoBean);
		lockiprequestdtoCall.callService();	
	}
	
	/**
	 * 验证指定的IPs都已经被解锁了，即不在黑名单
	 * @throws Exception 
	 */
	private void verifyIpsStatus(String[] ips) throws Exception{
	    for(int i=0; i<ips.length; i++){
	       boolean isLocked = blackLisWapper.isIPLocked(ips[i]);
	       Logger.verifyEquals(false, isLocked, "验证IP是否被锁定了");
	    }
	}
	
}